package com.resou.reader.base.v;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.utils.log.RsLog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IView {

    protected View mContentView;
    protected P presenter;

    Unbinder unbinder;
    protected Activity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializePresenter();
        RsLog.i(getClass().getSimpleName() + "---onCreate()");
    }

    /**
     * 同步M 、V、 P的生命周期
     */
    protected void initializePresenter() {
        initPresenter();
        if (presenter != null) {
            getLifecycle().addObserver(presenter);
            if (presenter.model != null) {
                getLifecycle().addObserver(presenter.model);
            }
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        RsLog.i(getClass().getSimpleName() + "---onCreateView()");
        mContentView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, mContentView);
        initViews();
        return mContentView;
    }

    protected abstract int getLayoutId();

    /**
     * 初始化View(设置Listener等等)
     */
    protected abstract void initViews();

    protected abstract void initPresenter();

    protected abstract void initData();


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RsLog.i(getClass().getSimpleName() + "---onActivityCreated()");
        initData();
    }

    @Override
    public void onStart() {
        super.onStart();
        RsLog.i(getClass().getSimpleName() + "---onStart()");
    }

    @Override
    public void onResume() {
        super.onResume();
        RsLog.i(getClass().getSimpleName() + "---onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        RsLog.i(getClass().getSimpleName() + "---onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        RsLog.i(getClass().getSimpleName() + "---onStop()");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RsLog.i(getClass().getSimpleName() + "---onDestroyView()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RsLog.i(getClass().getSimpleName() + "---onDestroy()");
        unbinder.unbind();
    }
}
